<?

/*
*
*   Excalibur Content Management System
*   Copyright © 2008 Egor "Sontan" Kuryanovich
*
*   Based on Explay Engine v2.0 by Golovdinov Alexander
*
*   Official site: www.excms.ru
*   Contact e-mail: support@excms.ru
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
*/

if ($EXCMS != "work") {
	header ("Location: ../../index.php");
	exit;
}

print '<ul id="top_nav"><li class="sel"><a href="admin.php">Панель администратора</a></li><li>Рассылка</li></ul>';

if ($_GET['send'] == true) {
	$group = $_POST['group'];
	$emails = $_POST['emails'];
	$author = $_POST['author'];
	$title = $_POST['title'];
	$body = $_POST['body'];
	if ($_POST['html'] == "true") $type = "text/html";
	else { $type = "text/plain"; $body = strip_tags($body); }
	
	if ($author != "" && $title != "" && $body != "") {
		if ($emails == "") {
			if ($group == "all") $get_users_emails = @mysql_query ("SELECT user_email FROM ".DB_PEREFIX."_users");
			if ($group == "users") $get_users_emails = @mysql_query ("SELECT user_email FROM ".DB_PEREFIX."_users WHERE user_group = 'user'");
			if ($group == "admins") $get_users_emails = @mysql_query ("SELECT user_email FROM ".DB_PEREFIX."_users WHERE user_group = 'admin'");
			
			if (@mysql_num_rows ($get_users_emails) > 0) {
				$total = mysql_num_rows ($get_users_emails);
				$count = 0;
				while (list($email) = mysql_fetch_array($get_users_emails)) {
					$email = strtolower ($email);
					if ($count % 10 == 0) sleep (3);
					if (@mail($email, $title, past_text($body, $author), "From: ".$SITE['site_name']." <$author>")) $count++;
				}
				site_message ("Отправлено <b>$count</b> писем из <b>$total</b> возможных");
			}
			else site_error ("Не удалось выбрать адреса пользователей из БД");
		}
		elseif ($emails != "") {
			$emails = explode (",", $emails);
			$total = count($emails);
			$count = 0;
			foreach ($emails as $email) {
				if ($count % 10 == 0) sleep (3);
				if (@mail(trim($email), $title, past_text($body, $author), "From: ".$SITE['site_name']." <$author>")) $count++;
			}
			site_message ("Отправлено <b>$count</b> писем из <b>$total</b> возможных");
		}
	}
	else site_error ("Не заполнены все поля");
}

$form = "<form style=\"margin:0px\" name=\"mailer\" method=\"post\" action=\"admin.php?do=mailer&send=true\">
<table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" class=\"main_text\">
	<tr>
		<td width=\"30%\">Группа пользователей:</td>
		<td>
			<select name=\"group\" class=\"form\">
				<option value=\"all\">Все пользователи</option>
				<option value=\"users\">Пользователи</option>
				<option value=\"admins\">Администраторы</option>
			</select>
		</td>
	</tr>
	<tr>
		<td>Введите адреса вручную через запятую:</td>
		<td>
			<input type=\"text\" name=\"emails\" style=\"width:290px\" class=\"form\" value=\"".$_POST['emails']."\">
			<a href=\"javascript:void(0)\" onmouseover=\"return overlib('При отправке не учитываются адреса пользователей вышеуказанных групп.', BELOW, RIGHT);\" onmouseout=\"return nd();\">?</a>
		</td>
	</tr>
	<tr>
		<td>Отправитель (e-mail):</td>
		<td><input type=\"text\" name=\"author\" style=\"width:230px\" class=\"form\" value=\"".$SITE['site_email']."\"></td>
	</tr>
	<tr>
		<td>Тема:<br></td>
		<td><input type=\"text\" name=\"title\" style=\"width:230px\" class=\"form\" value=\"".$_POST['title']."\"></td>
	</tr>
	<tr>
		<td valign=\"top\">Текст:<br></td>
		<td>
			<textarea name=\"body\" style=\"width:500px\" rows=\"12\" class=\"form\">".$_POST['body']."</textarea><br>
			[ <a href=\"javascript:void(0);\" onclick=\"document.mailer.body.rows=document.mailer.body.rows+10\">увеличить</a> ]
			[ <a href=\"javascript:void(0);\" onclick=\"javascript:document.mailer.body.rows=document.mailer.body.rows-10\">уменьшить</a> ]
		</td>
	</tr>
	<tr>
		<td colspan=\"2\"><label for=\"html\">Формат HTML</label> <span class=\"important_checkbox\"><input value=\"true\" id=\"html\" type=\"checkbox\" name=\"html\"></span></td>
	</tr>
	<tr>
		<td colspan=\"2\"><center><span class=\"important_button\"><input type=\"submit\" name=\"post\" value=\"Отправить\" class=\"button\"></span></td>
	</tr>
</table>
</form>";
past_table ("Рассылка", $form);

come_back ("панели администратора", "admin.php");

function past_text ($text, $author) {
	$body = "

$text

---------

С уважением, администрация ".$_SERVER['HTTP_HOST'];

	return $body;
}

?>
